<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false"  width="800">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item label="客户id" prop="customId">
        <el-input v-model="dataForm.customId" placeholder="客户id"  style="width: 400px;"></el-input>
      </el-form-item>
      <el-form-item label="方案日期" prop="planDate">
        <el-date-picker
          v-model="dataForm.planDate"
          type="datetime"
          placeholder="Pick a day"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="状态 0-不可用 1-可用" style="width: 240px">
          <el-option
            v-for="item in [{value: 1,label: '可用',},{value: 0,label: '不可用',}]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <el-table v-if="isUpdate" :data="dataForm.sportPlanDetails" stripe style="width: 100%">
      <el-table-column prop="sportName" label="食物名称" width="180"/>
      <el-table-column prop="duration" label="时长(分钟)" width="180"/>
      <el-table-column prop="trophy" label="热量(大卡)"/>
    </el-table>

    <template #footer>
      <div style="width: 350px">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const isUpdate = ref(false);

const dataForm = reactive({
  id: '',
  sportId: '',
  status: '',
  planDate: '',
  customId: '',
  deleted: '',
  sportPlanDetails: [],
  createDate: '',
  creator: '',
  updateDate: '',
  updater: ''
});

const rules = ref({
  sportId: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  status: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  planDate: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  customId: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  deleted: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  isUpdate.value = false

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    isUpdate.value = true
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/health/sportplan/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/health/sportplan", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
